package online.westonline.com.westonline.ui;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.easy.west.westclient.Constants;

import com.grandbroad.online.greendao.NodeBeanDao;

import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.adapter.NodeTypeListAdpter;
import online.westonline.com.westonline.base.BaseActivity;
import online.westonline.com.westonline.entry.NodeBean;
import online.westonline.com.westonline.entry.NodeTypeBean;
import online.westonline.com.westonline.utils.GreenDaoUtils;
import online.westonline.com.westonline.utils.UiUtlis;

public class NodeTypeActivity extends BaseActivity {
    @BindView(R.id.tv_topbar_title)
    TextView tvTopbarTitle;

    @BindView(R.id.tv_topbar_title_center)
    TextView tvTopbarTitleCenter;
    @BindView(R.id.tv_topbar_menu)
    TextView tvTopbarMenu;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recycler_nodetype_list)
    RecyclerView recyclerNodetypeList;
    @BindView(R.id.view_dercation)
    View viewDercation;
    @BindView(R.id.img_toolbar_menu)
    ImageView imgToolbarMenu;
    @BindView(R.id.btn_node_add)
    FloatingActionButton btnNodeAdd;
    private int QUERY_DONE = 10;
    private int WORK_DOWN = 11;
    private boolean isfirst=true;
    private NodeTypeListAdpter typeListAdpter;
    private List<NodeTypeBean> nodeTypeBeans = new ArrayList<>();
    private NodeBeanDao beanDao;
    private List<NodeBean> type_node = new ArrayList<>();
    private List<NodeBean> type_forgiving = new ArrayList<>();
    private List<NodeBean> type_remeber = new ArrayList<>();
    private List<NodeBean> type_aboutworks = new ArrayList<>();
    private List<NodeBean> type_words = new ArrayList<>();
    private Handler myhandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            //查询完了
            if (msg.what == QUERY_DONE) {
                updateData();
            }
            //数据拆分完成
            if (msg.what == WORK_DOWN) {
                //更新数据
                typeListAdpter.setTypeBeans(nodeTypeBeans);
                typeListAdpter.notifyDataSetChanged();
            }
            return true;
        }
    });


    /**
     * 更新列表数据
     */
    private void updateData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //拆分数据
                configdata(type_node, Constants.TYPE_NODE);
                configdata(type_forgiving, Constants.TYPE_FORGIVING);
                configdata(type_remeber, Constants.TYPE_REMENBER);
                configdata(type_words, Constants.TYPE_WORDS);
                configdata(type_aboutworks, Constants.TYPE_ABOUTWORKS);
                Message message = new Message();
                message.what = WORK_DOWN;
                myhandler.sendMessage(message);
                myhandler.obtainMessage();
            }
        }).start();

    }


    @Override
    protected void initview() {
        toolbar.setTitle("工作笔记");
        toolbar.setTitleTextColor(UiUtlis.getColor(R.color.colorMenuBtn));
        toolbar.setBackgroundColor(UiUtlis.getColor(R.color.colorAppTheme));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
        initrecycleview();
        initsession();
    }

    /**
     * 事件监听
     */
    private void initsession() {
        btnNodeAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(EditNodeActivity.class,false);
            }
        });

    }

    /**
     * 初始化视图
     */
    private void initrecycleview() {
        typeListAdpter = new NodeTypeListAdpter();
        recyclerNodetypeList.setLayoutManager(new LinearLayoutManager(this));
        recyclerNodetypeList.setAdapter(typeListAdpter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isfirst)
        {
            isfirst=false;
        }else
        {
            //并且情况集合
            nodeTypeBeans.removeAll(nodeTypeBeans);
            recyclerNodetypeList.removeAllViews();
            initdata();
        }

    }

    @Override
    protected void initdata() {
        beanDao = GreenDaoUtils.getInstance().getDaoSession().getNodeBeanDao();
        //查询根据type查询item
        //在子线程里去执行
        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        type_forgiving = getNodeByType(NodeBeanDao.Properties.Type.eq(Constants.TYPE_FORGIVING));
                        type_aboutworks = getNodeByType(NodeBeanDao.Properties.Type.eq(Constants.TYPE_ABOUTWORKS));
                        type_remeber = getNodeByType(NodeBeanDao.Properties.Type.eq(Constants.TYPE_REMENBER));
                        type_words = getNodeByType(NodeBeanDao.Properties.Type.eq(Constants.TYPE_WORDS));
                        type_node = getNodeByType(NodeBeanDao.Properties.Type.eq(Constants.TYPE_NODE));
                        Message message = new Message();
                        message.what = QUERY_DONE;
                        myhandler.sendMessage(message);
                        myhandler.obtainMessage();
                    }
                }
        ).start();


    }

    /**
     * 处理查询到的各类型的便签数据
     *
     * @param type
     * @param typeNode
     */
    private void configdata(List<NodeBean> type, String typeNode) {
        if (type.size() > 0) {
            NodeTypeBean nodeTypeBean = new NodeTypeBean();
            nodeTypeBean.setCount(type.size());
            nodeTypeBean.setType(typeNode);
            nodeTypeBeans.add(nodeTypeBean);

        } else {
            NodeTypeBean nodeTypeBean = new NodeTypeBean();
            nodeTypeBean.setCount(0);
            nodeTypeBean.setType(typeNode);
            nodeTypeBeans.add(nodeTypeBean);
        }
    }

    /**
     * 查询
     *
     * @param eq
     * @return
     */
    private List<NodeBean> getNodeByType(WhereCondition eq) {
        QueryBuilder<NodeBean> builder = beanDao.queryBuilder();
        builder.where(eq).build();
        return builder.list();
    }


    @Override
    protected int setlayout() {
        return R.layout.activity_node_type;
    }


}
